<template>
  <div class="filter-view">
    <!-- 导航部分 -->
    <div class="filter" :class="{isFixed: isShowMask}">
      <div class="filter-nav" v-for="(item,index) in filterViewDatas.navTab" :key="index" @click="clickNav(index)" :class="{'fontBold': index===currentIndex}">
        <span>{{ item.name}}</span>
        <i :class="'fa fa-'+item.icon" v-if="item.icon"></i>
      </div>

    </div>
    <!-- 排序 -->
    <div class="filter-extend" v-show="isShowMask">
      <ul v-for="(item,index) in filterViewDatas.sortBy" :key="index">
        <li @click="changeActive(index,item)">
          <span :class="{selectSort: activeIndex===index}">{{ item.name }}</span>
          <i class="fa fa-check" v-show="activeIndex===index"></i>
        </li>
      </ul>
    </div>
    <!-- 筛选 -->
    <div class="filter-extend" v-show="screenMask">
      <div class="filter-sort" v-for="(items,index) in filterViewDatas.screenBy" :key="index">
        <p>{{ items.title }}</p>
        <ul>
          <li v-for="(item,indey) in items.data" :key="indey" :class="{select: item.select}" @click="updateState(item,items)">
            <img :src="item.icon" alt="" v-if="item.icon">
            <span>{{ item.name }}</span>
          </li>
        </ul>
      </div>
      <div class="btn-operate">
        <button class="cancle" :class="{canclesure: cancelState}" @click="clearSelect">清空</button>
        <button class="sure" @click="filterSure">确定</button>
      </div>
    </div>
    <!-- 背景遮罩 -->
    <div class="mask" v-show="isShowMask || screenMask" @click="hideMask"></div>
  </div>
</template>

<script>
export default {
  name: 'FilterView',
  props: {
    filterViewData: Object
  },
  data() {
    return {
      currentIndex: 0,
      isShowMask: false,
      activeIndex: 0,
      screenMask: false,
      selectFilter: false
    }
  },
  computed: {
    filterViewDatas() {
      return this.filterViewData
    },
    // 点击取消按钮
    cancelState() {
      let state = false
      const length = Object.keys(this.filterViewDatas).length
      if (length !== 0) {
        this.filterViewData.screenBy.forEach(items => {
          items.data.forEach(item => {
            if (item.select) {
              state = true
            }
          })
        })
      }
      return state
    }
  },
  methods: {
    clickNav(index) {
      this.currentIndex = index
      switch (index) {
        case 0:
          this.isShowMask = true
          this.$emit('clickNav')
          // this.hideMask()
          break
        case 1:
          this.$emit('getSort', { condition: this.filterViewDatas.navTab[1].condition })
          // this.hideMask()
          break
        case 2:
          this.$emit('getSort', { condition: this.filterViewDatas.navTab[2].condition })
          // this.hideMask()
          break
        case 3:
          this.screenMask = true
          this.$emit('clickNav')
          break
        default:
          break
      }
    },
    hideMask() {
      this.isShowMask = false
      this.screenMask = false
      this.$emit('hideMask')
    },
    // 导航栏更改 激活状态
    changeActive(index, item) {
      this.activeIndex = index
      this.hideMask()
      this.$emit('getSort', { condition: item.code })
      this.filterViewDatas.navTab[0].name = item.name
    },
    // 点击筛选中的li
    updateState(value, item) {
      // 单选
      if (item.id !== 'MPI') {
        item.data.forEach(item => {
          item.select = false
        })
      }
      // 多选
      value.select = !value.select
    },
    clearSelect() {
      this.filterViewDatas.screenBy.forEach(items => {
        items.data.forEach(item => {
          item.select = false
        })
      })
    },
    // 点击确定键
    filterSure() {
      let selectItem = ''
      const selectItems = {
        MPI: '',
        offer: '',
        per: ''
      }
      this.filterViewDatas.screenBy.forEach(items => {
        items.data.forEach(item => {
          if (item.select) {
            if (items.id !== 'MPI') {
              // 单选
              // selectItem[items.id] = item.code
            }
            else {
              // 多选
              selectItem += item.code + ','
              selectItems[items.id] = selectItem
            }
          }
        })
      })
      this.$emit('getSort', selectItems)
      this.hideMask()
    }
  }
}
</script>

<style lang="less" scoped>
.filter-view {
  position: sticky;
  top: 42px;
  z-index: 999;
  margin-top: 10px;
  .filter {
    display: flex;
    justify-content: space-around;
    border-bottom: 1px solid #ccc;
    background-color: #fff;
    position: relative;

    z-index: 999;
    .filter-nav {
      position: relative;
      display: flex;
      align-items: center;
      font-size: 14px;
      padding: 10px 5px;
      color: #666;
      z-index: 999;
      i {
        margin-left: 5px;
        &::before {
          background-color: #fff;
        }
      }
    }
  }
  .filter-extend {
    position: relative;
    padding: 5px 10px;
    .filter-sort {
      p {
        font-size: 14px;
        margin: 0;
        margin-bottom: 10px;
      }
      ul {
        display: flex;
        flex-wrap: wrap;
        font-size: 12px;
        li {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 5px 10px;
          margin: 10px;
          width: 75px;
          background-color: #eee;
          img {
            width: 12px;
            height: 12px;
            margin-right: 5px;
          }
        }
      }
    }
    .btn-operate {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      display: flex;
      justify-content: center;
      height: 45px;
      .sure,
      .cancle {
        flex: 1;
        border: none;
        color: #fff;
        font-size: 16px;
      }
      .cancle {
        background-color: gray;
      }
      .sure {
        background-color: lightgreen;
      }
    }
  }
  .mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    transition: all 0.3s ease-in-out;
  }
  .fontBold {
    font-weight: 600;
    color: #333;
  }
  .filter-extend {
    position: relative;
    z-index: 10;
    background-color: #fff;
    ul {
      li {
        display: flex;
        padding: 12px;
        justify-content: space-between;
      }
    }
  }
  .fa-check {
    color: #009eef;
  }
  .selectSort {
    color: #009eef;
  }
  .select {
    color: #3190e8 !important;
    background-color: #edf5ff !important;
  }
  .canclesure {
    color: #333 !important;
    background-color: #ddd !important;
  }
}
</style>
